#!/usr/bin/env julia

using TongLun
using LinearAlgebra

"""
Example demonstrating cross-language interoperability with TongLun

This example shows how Julia and Python can share functions and data seamlessly.
"""

function main()
    println("TongLun Cross-Language Interoperability Example")
    println("=" ^ 50)
    
    # Export some Julia functions for other languages to use
    println("📤 Exporting Julia functions...")
    
    # Mathematical functions
    TongLun.export_fn("julia.add", (a, b) -> a + b)
    TongLun.export_fn("julia.factorial", factorial)
    TongLun.export_fn("julia.fibonacci", function(n)
        if n <= 1
            return n
        else
            return TongLun.call_fn("julia.fibonacci", n-1) + TongLun.call_fn("julia.fibonacci", n-2)
        end
    end)
    
    # Array processing functions
    TongLun.export_fn("julia.matrix_multiply", *)
    TongLun.export_fn("julia.eigen_values", function(matrix)
        eigen_result = eigen(matrix)
        return eigen_result.values
    end)
    
    # String processing
    TongLun.export_fn("julia.reverse_string", reverse)
    TongLun.export_fn("julia.word_count", function(text)
        words = split(text)
        return length(words)
    end)
    
    # Complex number operations
    TongLun.export_fn("julia.complex_magnitude", abs)
    TongLun.export_fn("julia.complex_phase", angle)
    
    println("✅ Julia functions exported successfully!")
    
    # Test calling our own exported functions
    println("\n🧪 Testing Julia function calls...")
    
    result1 = TongLun.call_fn("julia.add", 10, 32)
    println("julia.add(10, 32) = $result1")
    
    result2 = TongLun.call_fn("julia.factorial", 5)
    println("julia.factorial(5) = $result2")
    
    result3 = TongLun.call_fn("julia.fibonacci", 10)
    println("julia.fibonacci(10) = $result3")
    
    # Test array operations
    A = [1.0 2.0; 3.0 4.0]
    B = [5.0 6.0; 7.0 8.0]
    result4 = TongLun.call_fn("julia.matrix_multiply", A, B)
    println("julia.matrix_multiply(A, B) = ")
    display(result4)
    
    eigenvals = TongLun.call_fn("julia.eigen_values", A)
    println("julia.eigen_values(A) = $eigenvals")
    
    # Test string operations
    text = "Hello, TongLun world!"
    result5 = TongLun.call_fn("julia.reverse_string", text)
    println("julia.reverse_string(\"$text\") = \"$result5\"")
    
    word_count = TongLun.call_fn("julia.word_count", text)
    println("julia.word_count(\"$text\") = $word_count")
    
    # Test complex number operations
    c = 3.0 + 4.0im
    magnitude = TongLun.call_fn("julia.complex_magnitude", c)
    phase = TongLun.call_fn("julia.complex_phase", c)
    println("Complex number $c: magnitude = $magnitude, phase = $phase")
    
    println("\n✅ All Julia functions work correctly!")
    
    # Try to call Python functions if available
    println("\n🐍 Attempting to call Python functions...")
    
    try
        # These would only work if Python binding is also loaded
        python_result = TongLun.call_fn("python.eval", "2 + 3")
        println("python.eval('2 + 3') = $python_result")
        
        println("✅ Successfully called Python functions from Julia!")
    catch e
        println("⚠️  Python functions not available: $e")
        println("   (This is expected if Python binding is not loaded)")
    end
    
    println("\n🎯 Advanced Example: Scientific Computing Pipeline")
    println("-" ^ 40)
    
    # Create a more complex example involving multiple data types
    TongLun.export_fn("julia.analyze_data", function(data_array)
        # Statistical analysis
        n = length(data_array)
        mean_val = sum(data_array) / n
        variance = sum((data_array .- mean_val).^2) / (n - 1)
        std_dev = sqrt(variance)
        
        return (
            mean = mean_val,
            variance = variance,
            std_dev = std_dev,
            min = minimum(data_array),
            max = maximum(data_array)
        )
    end)
    
    # Test with sample data
    sample_data = randn(100)  # 100 random normal values
    stats = TongLun.call_fn("julia.analyze_data", sample_data)
    println("Statistical analysis of random data:")
    println("  Mean: $(stats.mean)")
    println("  Std Dev: $(stats.std_dev)")
    println("  Min: $(stats.min)")
    println("  Max: $(stats.max)")
    
    println("\n🎉 Cross-language interoperability demonstration complete!")
    println("Julia functions are now available for Python, R, and other languages to call.")
end

if abspath(PROGRAM_FILE) == @__FILE__
    main()
end 